package com.example.base.http

import com.blankj.utilcode.util.SPUtils
import com.zyj.retrofit.adapter.FlowCallAdapterFactory
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

object RetFitUtils {
    fun retrofit():Retrofit{
        val client = OkHttpClient.Builder()
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .addInterceptor {
                val newBuilder = it.request().newBuilder()
                newBuilder.addHeader("accessId", "147dc7537b104be8add33fcc942ffde0")
                    .addHeader("token",
                        SPUtils.getInstance().getString(Cons.TOKEN,"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJsciJ9.U4skq0o-_2oDb11fZLMDB-SfMDwQvVDpWFHg3D9UpR8"))
                it.proceed(newBuilder.build())
            }
            .build()

        val retrofit = Retrofit.Builder()
            .client(client)
            .baseUrl(Cons.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(FlowCallAdapterFactory.create())
            .build()
        return retrofit
    }

   /* val okHttpClient by lazy {
        OkHttpClient.Builder()
            .connectTimeout(10, TimeUnit.SECONDS)
            .writeTimeout(10,TimeUnit.SECONDS)
            .readTimeout(10,TimeUnit.SECONDS)
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .addInterceptor {
                val request = it.request().newBuilder()
                    .addHeader("accessId", "db73e0151a8e41ec8bfd4eb615029dc6")
                    .addHeader("token",
                        SPUtils.getInstance().getString(Cons.TOKEN,"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJsciJ9.U4skq0o-_2oDb11fZLMDB-SfMDwQvVDpWFHg3D9UpR8"))
                    .build()
                it.proceed(request)
            }
            .build()
    }

    val retrofit by lazy {
        Retrofit.Builder()
            .baseUrl(Cons.BASE_URL)
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(FlowCallAdapterFactory.create())
            .build()
    }*/
}